package com.yanqu.road.server.cmd.activity.xingyoushanshui.cross;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_XING_YOU_SHAN_SHUI_USE_ITEM_FROM_CROSS,desc = "使用活动道具-跨服回包")
public class ShanShuiUseItemFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        XingYouShanShuiProto.CrossShanShuiUseItemRsp rsp = XingYouShanShuiProto.CrossShanShuiUseItemRsp.parseFrom(packet.getBytes());

        // 预扣除
        player.getModule(PreReduceModule.class).restorePreReduce(rsp.getConsume());

        XingYouShanShuiProto.ShanShuiUseItemRsp clientMsg = rsp.getClientMsg();
        if(clientMsg.getRet() == 0){
            if(rsp.hasConsume()){
                player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(rsp.getConsume()), eLogMoneyType.XingYouShanShuiActivity, eLogMoneyType.XingYouShanShuiUseItemConsume);
            }
            if(clientMsg.hasAward()){
                player.getModule(CurrencyModule.class).addCurrency(clientMsg.getAward(), eLogMoneyType.XingYouShanShuiActivity, eLogMoneyType.XingYouShanShuiUseItemReward);
            }
        }

        player.sendPacket(Protocol.U_XING_YOU_SHAN_SHUI_USE_ITEM, clientMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
